package org.thirdHero.core.config;

import org.thirdHero.core.exception.TokenErrorException;
import org.thirdHero.core.util.ResponseUtil;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

@ControllerAdvice
public class GlobalExceptionHandler {


    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    @ResponseBody
    public Object argumentHandler(MethodArgumentTypeMismatchException e){
        e.printStackTrace();
        return ResponseUtil.badArgumentValue();
    }

    /**
     * 处理全局异常
     * TODO: 记录全局异常类
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object exceptionHandler(Exception e){
        if(e instanceof TokenErrorException){
            TokenErrorException exception = (TokenErrorException) e;
            return ResponseUtil.fail(exception.getCode(),e.getMessage());
        }
        e.printStackTrace();
        return ResponseUtil.serious();
    }

}
